//
//  ReadDetailsCollectionCell.m
//  aMoment
//
//  Created by dllo on 15/10/11.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "ReadDetailsCollectionCell.h"
#import "AllHeaderFiles.h"

@implementation ReadDetailsCollectionCell

- (void)dealloc
{
    [_tableView release];
    [_dataSourceArray release];
    [_progressView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSourceArray = [NSMutableArray array];
        
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        [self.contentView addSubview:_tableView];
        [_tableView release];
        
        _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _start = 0;
            [self getDataWithBodyString:_bodyString];
        }];
//        [_tableView.header beginRefreshing];
        _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _start += 10;
            [self getDataWithBodyString:[_bodyString stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", _start]]];
        }];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.contentView.Height);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"%ld", _dataSourceArray.count);
    return _dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"table";
    ReadDetailsCollectionCellOfTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[ReadDetailsCollectionCellOfTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    if (_dataSourceArray.count) {
        cell.readDetails = _dataSourceArray[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135 * FIT_HEIGHT;
}

#pragma mark 重写set
- (void)setBodyString:(NSString *)bodyString
{
    if (_bodyString != bodyString) {
        [_bodyString release];
        _bodyString = [bodyString retain];
    }
    //创建加载中提示视图
    self.progressView = [LoadingProgress createViewWithSuperviewFrame:self.contentView.frame];
    [self.contentView addSubview:_progressView];
    [_progressView release];
    
    [self getDataWithBodyString:_bodyString];
}

#pragma mark 数据解析
- (void)getDataWithBodyString:(NSString *)bodyString
{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        self.dataSourceArray = [NSMutableArray array];
//    });
    [NetworkRequest getDataWithURLString:URLSTRING_READ_DETAILS BodyString:bodyString DataBlock:^(id data) {
        //移除加载中提示视图
        [_progressView removeFromSuperview];
        
        if ([bodyString containsString:@"start=0"]) {
            [_dataSourceArray removeAllObjects];
        }
        NSArray *listArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dict in listArray) {
            ReadDetails *readDetails = [[[ReadDetails alloc] init] autorelease];
            [readDetails setValuesForKeysWithDictionary:dict];
            [_dataSourceArray addObject:readDetails];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadDetailsInfoViewController *readDetailsInfoVC = [[ReadDetailsInfoViewController alloc] init];
    [(UINavigationController *)self.contentView.window.rootViewController pushViewController:readDetailsInfoVC animated:YES];
    readDetailsInfoVC.contentId = [NSString stringWithFormat:@"%@", [_dataSourceArray[indexPath.row] Id]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
